package bible.sunflower.work.yinfeng.http;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.dom4j.DocumentHelper;

import bible.sunflower.util.XMLUtil;


public class HttpClient4Hoyi {
	
	private static HttpPost createPost(String requestUrl, String cookieStr){		
		HttpPost httpost =  new HttpPost( requestUrl );
		httpost.addHeader("connection","keep-alive");
		httpost.addHeader("Cache-Control","no-cache");		
		if( cookieStr != null && cookieStr.trim().length() > 0 ){
			httpost.setHeader("Cookie", cookieStr );
		}	
		return httpost;
	}

	public static String invokeLogicflow(String requesturl, Map<String, Object> paramMap, String cookieStr){
		String result = "";
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httpost = null;
		try {
			httpost = createPost( requesturl, cookieStr );
			httpost.addHeader("_eosajax", "xml");
			httpost.addHeader("X_REQUESTED_WITH","XMLHttpRequest");
			httpost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		
			String ajaxParam = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root><params>";
			if( paramMap != null && paramMap.size() > 0 ){
				for(String key : paramMap.keySet() ){
					ajaxParam += "<param><key>" + key + "</key><value>" + paramMap.get(key) + "</value></param>";
				}
			}
			ajaxParam += "</params><data></data></root>";
			List<NameValuePair> paramList = new ArrayList<NameValuePair>(); 
			paramList.add(new BasicNameValuePair("__ajaxParam", ajaxParam)); 
			httpost.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8)); 
	
			System.out.println("executing request: " + httpost.getURI().toString() );
	        ResponseHandler<String> responseHandler = new BasicResponseHandler();
	        String responseBody = httpclient.execute(httpost, responseHandler);
	        //System.out.println("----------------------------------------");
	        System.out.println("response body:"+ responseBody);
	        
	        //System.out.println("----------------------------------------");
	        Map<String, Object> map = XMLUtil.Dom2Map( DocumentHelper.parseText( responseBody));
	        result = JSONObject.fromObject(map).toString();
	        System.out.println( "json str :" + result );
	        //System.out.println("----------------------------------------");
		    } catch(Exception e){
		    	e.printStackTrace();
	    }finally {
	        httpclient.getConnectionManager().shutdown();
	    }	
		return result;
	}

}
